import time

from common.Utils import FSDriver
from selenium.webdriver.common.by import By


# 个人中心
class privatecenter:
    url = ""
    driver = ""

    def __init__(self):
        self.url = "http://127.0.0.1:58080/index.html"
        self.driver = FSDriver.driver
        self.driver.get(self.url)
        self.driver.implicitly_wait(5)


    # 个人中心入口
    def user_entry(self):
        # 定位
        dingwei = self.driver.find_element(By.CSS_SELECTOR, 'body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div')
        self.driver.execute_script("arguments[0].scrollIntoView(true);", dingwei)
        time.sleep(1)

        self.driver.find_element(By.CSS_SELECTOR, '#index_nav_avatar').click()
        self.driver.find_element(By.CSS_SELECTOR, '#index_user_settings').click()
        assert self.driver.find_element(By.CSS_SELECTOR, '#settings_nickname')

    # 修改头像
    def revise_headshot(self):
        self.user_entry()

    # 修改昵称
    def revise_nickname(self):
        self.user_entry()
        # 跳转到修改昵称位置
        dianjianniu = self.driver.find_element(By.CSS_SELECTOR, '#setting_submit_nickname')
        self.driver.execute_script("arguments[0].scrollIntoView(true);", dianjianniu)
        time.sleep(1)

        # 不修改内容直接点击修改
        self.driver.find_element(By.CSS_SELECTOR, '#setting_submit_nickname').click()
        assert self.driver.find_element(By.CSS_SELECTOR, 'body > div.jq-toast-wrap.bottom-right > div')

        # 修改内容为空
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_nickname').clear()
        self.driver.find_element(By.CSS_SELECTOR, '#setting_submit_nickname').click()

        # 正确修改
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_nickname').clear()
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_nickname').send_keys('haitao')
        self.driver.find_element(By.CSS_SELECTOR, '#setting_submit_nickname').click()
        # 修改回去
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_nickname').clear()
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_nickname').send_keys('毕书尽')
        self.driver.find_element(By.CSS_SELECTOR, '#setting_submit_nickname').click()

    # 修改邮箱
    def revise_email(self):
        self.user_entry()
        # 跳转到修改邮箱位置
        dianjianniu = self.driver.find_element(By.CSS_SELECTOR, '#setting_submit_email')
        self.driver.execute_script("arguments[0].scrollIntoView(true);", dianjianniu)
        time.sleep(1)

        # 邮箱内容为空修改
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_email').clear()
        self.driver.find_element(By.CSS_SELECTOR, '#setting_submit_email').click()

        # 错误修改
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_email').clear()
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_email').send_keys('123')
        self.driver.find_element(By.CSS_SELECTOR, '#setting_submit_email').click()

        assert self.driver.find_element(By.CSS_SELECTOR, 'body > div.jq-toast-wrap.bottom-right > div > h2')

        # 正确修改
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_email').clear()
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_email').send_keys('3033009924@qq.com')

        FSDriver.getscreeshot()
        self.driver.find_element(By.CSS_SELECTOR, '#setting_submit_email').click()
        assert self.driver.find_element(By.CSS_SELECTOR, 'body > div.jq-toast-wrap.bottom-right > div')

        # 修改回去
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_email').clear()
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_email').send_keys('123')
        self.driver.find_element(By.CSS_SELECTOR, '#setting_submit_email').click()

    # 修改电话
    def revise_phone(self):
        self.user_entry()
        # 定位
        dianjianniu = self.driver.find_element(By.CSS_SELECTOR, '#setting_submit_phoneNum')
        self.driver.execute_script("arguments[0].scrollIntoView(true);", dianjianniu)
        time.sleep(1)

        # 内容为空修改
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_phoneNum').clear()
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_phoneNum').click()

        # 错误修改
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_phoneNum').clear()
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_phoneNum').send_keys('13911111111')
        self.driver.find_element(By.CSS_SELECTOR, '#setting_submit_phoneNum').click()
        FSDriver.getscreeshot()

        # 正确修改
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_phoneNum').clear()
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_phoneNum').send_keys('18311111813')
        self.driver.find_element(By.CSS_SELECTOR, '#setting_submit_phoneNum').click()
        FSDriver.getscreeshot()
        assert self.driver.find_element(By.CSS_SELECTOR, 'body > div.jq-toast-wrap.bottom-right > div')

        # 修改回去
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_phoneNum').clear()
        self.driver.find_element(By.CSS_SELECTOR, '#setting_input_phoneNum').send_keys('13911111111')
        self.driver.find_element(By.CSS_SELECTOR, '#setting_submit_phoneNum').click()
        FSDriver.getscreeshot()

    # 修改密码框内容清空
    def revise_password_clear(self):
        self.driver.find_element(By.CSS_SELECTOR, '#settings_input_oldPassword').clear()
        self.driver.find_element(By.CSS_SELECTOR, '#settings_input_newPassword').clear()
        self.driver.find_element(By.CSS_SELECTOR, '#settings_input_passwordRepeat').clear()

    # 修改密码
    def revise_password(self):
        self.user_entry()
        # 定位
        dingwei = self.driver.find_element(By.CSS_SELECTOR,
                                           '#bit-forum-content > div.page-body > div > div > div > div.col.d-flex.flex-column > div > h3:nth-child(13)')
        self.driver.execute_script("arguments[0].scrollIntoView(true);", dingwei)
        time.sleep(1)

        # 为空修改
        self.driver.find_element(By.CSS_SELECTOR, '#settings_submit_password').click()
        assert self.driver.current_url == 'http://127.0.0.1:58080/index.html'

        # 错误修改 -> 原密码错误
        self.revise_password_clear()
        self.driver.find_element(By.CSS_SELECTOR, '#settings_input_oldPassword').send_keys('qwe')
        self.driver.find_element(By.CSS_SELECTOR, '#settings_input_newPassword').send_keys('123qwe')
        self.driver.find_element(By.CSS_SELECTOR, '#settings_input_passwordRepeat').send_keys('123qwe')
        self.driver.find_element(By.CSS_SELECTOR, '#settings_submit_password').click()

        assert self.driver.find_element(By.CSS_SELECTOR, 'body > div.jq-toast-wrap.bottom-right > div')

        # 错误修改 -> 两次密码不符
        self.revise_password_clear()
        self.driver.find_element(By.CSS_SELECTOR, '#settings_input_oldPassword').send_keys('qwe')
        self.driver.find_element(By.CSS_SELECTOR, '#settings_input_newPassword').send_keys('123qwe')
        self.driver.find_element(By.CSS_SELECTOR, '#settings_input_passwordRepeat').send_keys('123456')
        self.driver.find_element(By.CSS_SELECTOR, '#settings_submit_password').click()

        assert self.driver.find_element(By.CSS_SELECTOR, 'body > div.jq-toast-wrap.bottom-right > div')

        # 错误修改 -> 两次密码相符但违规
        self.revise_password_clear()
        self.driver.find_element(By.CSS_SELECTOR, '#settings_input_oldPassword').send_keys('qwe')
        self.driver.find_element(By.CSS_SELECTOR, '#settings_submit_password').click()
        assert self.driver.current_url == 'http://127.0.0.1:58080/index.html'

        # 正确修改 -> 网页回退应该不能直接更改密码
        self.revise_password_clear()
        self.driver.find_element(By.CSS_SELECTOR, '#settings_input_oldPassword').send_keys('123')
        self.driver.find_element(By.CSS_SELECTOR, '#settings_input_newPassword').send_keys('123456')
        self.driver.find_element(By.CSS_SELECTOR, '#settings_input_passwordRepeat').send_keys('123456')
        self.driver.find_element(By.CSS_SELECTOR, '#settings_submit_password').click()
        # 修改成功退回登录首页
        # 未登录尝试修改密码
        FSDriver.getscreeshot()
        self.driver.back()
        assert self.driver.find_element(By.CSS_SELECTOR, 'body > div.jq-toast-wrap.bottom-right > div')

    # 修改密码为原始的
    def password_reposition(self):
        FSDriver.getscreeshot()
        self.user_entry()
        # 定位
        dingwei = self.driver.find_element(By.CSS_SELECTOR,
                                           '#bit-forum-content > div.page-body > div > div > div > div.col.d-flex.flex-column > div > div:nth-child(14) > div')
        self.driver.execute_script("arguments[0].scrollIntoView(true);", dingwei)
        time.sleep(1)
        FSDriver.getscreeshot()

        self.driver.find_element(By.CSS_SELECTOR, '#settings_input_oldPassword').send_keys('123456')
        self.driver.find_element(By.CSS_SELECTOR, '#settings_input_newPassword').send_keys('123')
        self.driver.find_element(By.CSS_SELECTOR, '#settings_input_passwordRepeat').send_keys('123')
        FSDriver.getscreeshot()
        self.driver.find_element(By.CSS_SELECTOR, '#settings_submit_password').click()

    # 修改简介
    def revise_synopsis(self):
        self.user_entry()
        dingwei = self.driver.find_element(By.CSS_SELECTOR, '#settings_textarea_remark')
        self.driver.execute_script("arguments[0].scrollIntoView(true);", dingwei)
        time.sleep(1)
        # 错误修改 不改内容直接点击
        self.driver.find_element(By.CSS_SELECTOR, '#settings_submit_remark').click()
        # 内容为空
        self.driver.find_element(By.CSS_SELECTOR, '#settings_textarea_remark').clear()
        self.driver.find_element(By.CSS_SELECTOR, '#settings_submit_remark').click()

        # 正确修改
        self.driver.find_element(By.CSS_SELECTOR, '#settings_textarea_remark').send_keys("hello")
        self.driver.find_element(By.CSS_SELECTOR, '#settings_submit_remark').click()
        assert self.driver.find_element(By.CSS_SELECTOR, 'body > div.jq-toast-wrap.bottom-right > div')

        # 修改回去
        self.driver.find_element(By.CSS_SELECTOR, '#settings_textarea_remark').clear()
        self.driver.find_element(By.CSS_SELECTOR, '#settings_textarea_remark').send_keys("hi 大家好")
        self.driver.find_element(By.CSS_SELECTOR, '#settings_submit_remark').click()

    def revise_privatecenter(self):
        self.driver.maximize_window()
        self.revise_nickname()
        self.revise_email()
        self.revise_phone()
        self.revise_synopsis()

        # 修改密码之后登录状态为退出
        self.revise_password()















